﻿#region usings
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using InsuranceAdjuster.OccasionallyConnected;
using System.ComponentModel;
using GalaSoft.MvvmLight.Command;
using InsuranceAdjuster.Views;
#endregion
namespace InsuranceAdjuster.ViewModels {
    public class StatusBarViewModel : ViewModelBase {
        public RelayCommand ShowSyncDialogCommand { get; set; }
        public RelayCommand SyncNowCommand { get; set; }

        public StatusBarViewModel() {
            EntityManager.PropertyChanged += PropertyChanged;
        }

        public int MessagesPending {
            get {
                return EntityManager.MessagesPending;
            }
        }
        public int MessagesInFlight {
            get {
                return EntityManager.MessagesInFlight;
            }
        }
        public int EntitiesInQueue {
            get {
                return EntityManager.EntitiesInQueue;
            }
        }


        protected override void RegisterCommands() {
            ShowSyncDialogCommand = new RelayCommand(() => {
                Deployment.Current.Dispatcher.BeginInvoke(() => {
                    //var MyChild = new ChildWindow();
                    //MyChild.Content = new SyncStatusView();
                    //MyChild.Show();

                    var MyChild = new FloatableWindow();
                    var MyPanel = new Grid();

                    ((App.Current.RootVisual as InsuranceAdjuster.Controls.BusyIndicator).Content as Grid).Children.Add(MyPanel);
                    //MyChild.ParentLayoutRoot = (((App.Current.RootVisual as InsuranceAdjuster.Controls.BusyIndicator).Content as Grid).Children[0] as MainPage).LayoutRoot;
                    MyChild.ParentLayoutRoot = MyPanel;
                    MyChild.Title = "Sync Status";
                    MyChild.Content = new SyncStatusView();
                    MyChild.MinWidth = 400;
                    MyChild.Width = 640;
                    MyChild.MinHeight = 350;
                    MyChild.Height = 520;

                    MyChild.Show();
                });
            });
            SyncNowCommand = new RelayCommand(() => {
                EntityManager.CacheMonitor.SyncNow();
            });
        }
        private void PropertyChanged(object sender, PropertyChangedEventArgs e) {
            switch (e.PropertyName) {
                case "EntitiesInQueue":
                    RaisePropertyChanged("EntitiesInQueue");
                    break;
                case "MessagesInFlight":
                    RaisePropertyChanged("MessagesInFlight");
                    break;
                case "MessagesPending":
                    RaisePropertyChanged("MessagesPending");
                    break;
            }
        }
    }
}
